﻿Public Class SearchKeyword

    Private _AndWordsText As String
    Public Property AndWordsText() As String
        Get
            Return _AndWordsText
        End Get
        Set(ByVal value As String)
            If value = _AndWordsText Then
                Exit Property
            End If

            _AndWordsText = value
            AndWords.Clear()

            If value Is Nothing Then
                Exit Property
            End If

            Dim t = New NormalizedText(value)
            For Each w In t.ToString.Split(New Char() {" "c}, StringSplitOptions.RemoveEmptyEntries)
                AndWords.Add(w)
            Next

        End Set
    End Property

    Private _OrWordsText As String
    Public Property OrWordsText() As String
        Get
            Return _OrWordsText
        End Get
        Set(ByVal value As String)
            If value = _OrWordsText Then
                Exit Property
            End If

            _OrWordsText = value
            OrWords.Clear()

            If value Is Nothing Then
                Exit Property
            End If

            Dim t = New NormalizedText(value)
            For Each w In t.ToString.Split(New Char() {" "c}, StringSplitOptions.RemoveEmptyEntries)
                OrWords.Add(w)
            Next

        End Set
    End Property

    Private _NotWordsText As String
    Public Property NotWordsText() As String
        Get
            Return _NotWordsText
        End Get
        Set(ByVal value As String)
            If value = _NotWordsText Then
                Exit Property
            End If

            _NotWordsText = value
            NotWords.Clear()

            If value Is Nothing Then
                Exit Property
            End If

            Dim t = New NormalizedText(value)
            For Each w In t.ToString.Split(New Char() {" "c}, StringSplitOptions.RemoveEmptyEntries)
                NotWords.Add(w)
            Next

        End Set
    End Property

    Private _RegExPattern As String
    Public Property RegExPattern() As String
        Get
            Return _RegExPattern
        End Get
        Set(ByVal value As String)
            _RegExPattern = value
        End Set
    End Property


    Private _AndWords As New List(Of String)
    <Xml.Serialization.XmlIgnore()> _
    Public ReadOnly Property AndWords() As IList(Of String)
        Get
            Return _AndWords
        End Get
    End Property

    Private _OrWords As New List(Of String)
    <Xml.Serialization.XmlIgnore()> _
    Public ReadOnly Property OrWords() As IList(Of String)
        Get
            Return _OrWords
        End Get
    End Property

    Private _NotWords As New List(Of String)
    <Xml.Serialization.XmlIgnore()> _
    Public ReadOnly Property NotWords() As IList(Of String)
        Get
            Return _NotWords
        End Get
    End Property


    Public Function IsHit(ByVal program As LiveProgram) As Boolean
        Dim sources = New String() { _
            program.NormalizedTitle, _
            program.NormalizedDescription, _
            program.NormalizedCasterName}

        Dim contains As Boolean

        For Each w In AndWords
            contains = False

            For Each s In sources

                If s.Contains(w) Then
                    contains = True
                    Exit For
                End If

            Next

            If Not contains Then
                Return False
            End If
        Next

        contains = False
        For Each w In OrWords
            For Each s In sources

                If s.Contains(w) Then
                    contains = True
                    Exit For
                End If

            Next

            If contains Then
                Exit For
            End If
        Next

        If Not contains AndAlso OrWords.Count > 0 Then
            Return False
        End If


        For Each w In NotWords
            For Each s In sources
                If s.Contains(w) Then
                    Return False
                End If
            Next
        Next

        If RegExPattern <> "" Then
            Dim match As Boolean = False
            For Each s In sources
                Try
                    If System.Text.RegularExpressions.Regex.IsMatch(s, RegExPattern) Then
                        match = True
                        Exit For
                    End If
                Catch ex As Exception

                End Try
            Next

            If Not match Then
                Return False
            End If
        End If

        Return True

    End Function


End Class